<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">小区名</label>
        <el-input v-model="query.communityName" clearable placeholder="小区名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">小区地址</label>
        <el-input v-model="query.communityAddress" clearable placeholder="小区地址" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">城市</label>
        <el-input v-model="query.city" clearable placeholder="城市" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="小区名" prop="communityName">
            <el-input v-model="form.communityName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="小区地址" prop="communityAddress">
            <el-input v-model="form.communityAddress" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="城市" prop="city">
            <el-input v-model="form.city" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="服务热线" prop="serviceHotline">
            <el-input v-model="form.serviceHotline" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工作时间" prop="workingHours">
            <el-input v-model="form.workingHours" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="服务邮箱" prop="serviceMailbox">
            <el-input v-model="form.serviceMailbox" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="app菜单">
            <el-select v-model="form.appMenuList" multiple filterable placeholder="请选择">
              <el-option
                v-for="item in dict.app_menu"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="createBy" label="创建者" />
        <el-table-column prop="communityName" label="小区名" />
        <el-table-column prop="communityAddress" label="小区地址" />
        <el-table-column prop="code" label="小区code码" />
        <el-table-column prop="city" label="城市" />
        <el-table-column prop="serviceHotline" label="服务热线" />
        <el-table-column prop="workingHours" label="工作时间" />
        <el-table-column prop="serviceMailbox" label="服务邮箱" />
        <el-table-column prop="createTime" label="创建日期" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column v-if="checkPer(['admin','regionManage:edit','regionManage:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudRegionManage from '@/api/system/regionManage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { validEmail } from '@/utils/validate'

const defaultForm = { id: null, createTime: null, updateTime: null, createBy: null, updateBy: null, deleted: null, serviceMailbox: null, workingHours: null, communityName: null, serviceHotline: null, communityAddress: null, code: null, city: null, appMenuList: null }
export default {
  name: 'RegionManage',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '小区', url: 'api/regionManage', idField: 'id', sort: 'id,desc', crudMethod: { ...crudRegionManage }})
  },
  dicts: ['app_menu'],
  data() {
    // 自定义验证
    const isValidEmail = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入服务邮箱'))
      } else if (!validEmail(value)) {
        callback(new Error('请输入正确的邮箱'))
      } else {
        callback()
      }
    }
    return {
      permission: {
        add: ['admin', 'regionManage:add'],
        edit: ['admin', 'regionManage:edit'],
        del: ['admin', 'regionManage:del']
      },
      rules: {
        communityName: [
          { required: true, message: '小区名不能为空', trigger: 'blur' }
        ],
        communityAddress: [
          { required: true, message: '小区地址不能为空', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '城市不能为空', trigger: 'blur' }
        ],
        serviceHotline: [
          { required: true, message: '服务热线不能为空', trigger: 'blur' }
        ],
        workingHours: [
          { required: true, message: '工作时间不能为空', trigger: 'blur' }
        ],
        serviceMailbox: [
          { required: true, validator: isValidEmail, trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'communityName', display_name: '小区名' },
        { key: 'communityAddress', display_name: '小区地址' },
        { key: 'city', display_name: '城市' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.optShow.download = false
      return true
    }
  }
}
</script>

<style scoped>

</style>
